# code_blocks.py
def code_block_1():
    print("正在执行代码块1")
    import calendar
    year = int(input("请输入年份: "))
    month = int(input("请输入月份: "))
    day = int(input("请输入日期: "))
    def day_of_year_using_calendar(year, month, day):
        day_count = 0
        for m in range(1, month):  
            _, days_in_month = calendar.monthrange(year, m)
            day_count += days_in_month
        day_count += day  
        return day_count
    print(f"{year}年{month}月{day}日是这一年的第{day_of_year_using_calendar(year, month, day)}天")

def code_block_2():
    print("正在执行代码块2")
    def is_prime(n, primes):
        if n <= 1:
            return False
        for prime in primes:
            if prime * prime > n:
                break  
            if n % prime == 0:
                return False  
        return True  

    def find_primes_up_to(limit):
        primes = [] 
        for num in range(2, limit + 1):
            if is_prime(num, primes):
                primes.append(num) 
        return primes
    limit = 1000
    primes = find_primes_up_to(limit) 
    print(f"1000以内的素数个数为: {len(primes)}")
    print("1000以内的所有素数为:")
    for prime in primes:
        print(prime, end=' ')

def code_block_3():
    print("正在执行代码块3")
    score = float(input("请输入学生的成绩："))
    grade = 'A' if score >= 90  else ('B' if score >= 60 else 'C')
    print("学生的成绩等级是：", grade)

def code_block_4():
    print("正在执行代码块4")
    def count_characters(s):
        letters = sum(1 for c in s if c.isalpha())
        spaces = sum(1 for c in s if c.isspace())
        digits = sum(1 for c in s if c.isdigit())
        others = sum(1 for c in s if not c.isalnum() and not c.isspace())
        return letters, spaces, digits, others

    input_string = input("请输入一行字符串：")
    letters, spaces, digits, others = count_characters(input_string)
    
    print(f"英文字母个数：{letters}")
    print(f"空格个数：{spaces}")
    print(f"数字个数：{digits}")
    print(f"其他字符个数：{others}")

def code_block_5():
    print("正在执行代码块5")
    def find_perfect_numbers(n):
        perfect_numbers = []  
        for num in range(1, n+1):
            factors_sum = 0  
            for i in range(1, num):
                if num % i == 0:  
                    factors_sum += i  
            if factors_sum == num:  
                perfect_numbers.append(num)  
        return perfect_numbers
    perfect_numbers = find_perfect_numbers(1000)
    print("1000以内的完数有：", perfect_numbers)

def code_block_6():
    print("正在执行代码块6")
    def factorial(n):
        if n == 0:
            return 1
        else:
            return n * factorial(n-1)

    N = int(input("请输入一个正整数N："))
    def sum_of_factorials(N):
        sum = 1  
        for i in range(2, N+1):
            sum += factorial(i)
        return sum
    print(f"1 + 2! + 3! + ... + {N}! 的值是：{sum_of_factorials(N)}")

def code_block_7():
    print("正在执行代码块7")
    N = int(input("请输入一个奇数N："))
    def print_diamond(N):
        if N % 2 == 0:
            print("请输入一个奇数")
            return
        for i in range(N // 2 + 1):
            print(' ' * (N // 2 - i) + '*' * (2 * i + 1))
        for i in range(N // 2 - 1, -1, -1):
            print(' ' * (N // 2 - i) + '*' * (2 * i + 1))
    print_diamond(N)

def code_block_8():
    print("正在执行代码块8")
    def get_numbers():
        numbers = []
        print("请输入10个数，每输入一个数后按回车键：")
        for i in range(10):
            while True:
                try:
                    num = float(input(f"输入第{i+1}个数："))
                    numbers.append(num)
                    break
                except ValueError:
                    print("输入无效，请输入一个数字。")
        return numbers

    def sort_numbers(numbers):
        return sorted(numbers)
    numbers = get_numbers()
    sorted_numbers = sort_numbers(numbers)
    print("输入的数列:", numbers)
    print("排序后的数列:", sorted_numbers)

def code_block_9():
    print("正在执行代码块9")
    def binary_search_insert(arr, target):
        low, high = 0, len(arr) - 1
        while low <= high:
            mid = (low + high) // 2
            if arr[mid] == target:
                return mid 
            elif arr[mid] < target:
                low = mid + 1
            else:
                high = mid - 1
        return low  

    def insert_into_sorted_array(arr, target):
        index = binary_search_insert(arr, target)
        arr.insert(index, target)
        return arr
    
    sorted_array = [1, 4, 8, 56, 545, 879, 999, 4266, 45645, 4562425]  
    number_to_insert = int(input("请输入一个数："))  
    new_array = insert_into_sorted_array(sorted_array, number_to_insert)
    print("插入后的数组：", new_array)

def code_block_10():
    print("正在执行代码块10")
    def last_person_standing(n):
        people = list(range(1, n+1))
        index = 0
        while len(people) > 1:
            index = (index + 2) % len(people)
            people.pop(index)
        return people[0]

    n = int(input("请输入一个数字n："))
    print("最后留下的是原来第{}号的那位。".format(last_person_standing(n)))